exports.feedbackAdd = (req, res) => {

    var bodyParams = req.routeInfo.bodyParams;

    var resultData = {};

    var is_master = bodyParams.is_master;

    var u_id = bodyParams.u_id;

    var u_fid = 0;

    var u_nickname = 0;

    var u_leader = 0;

    var openid = [];

    //查询当前用户所在节点的父节点
    function queryParentNode(cb) {

        if (is_master > 0) {
            u_fid = u_id;
            u_nickname = bodyParams.sq_nickname;
            return cb();
        }

        R.SEND_HTTP(
            req,
            res,
            {
                url: CORE_APIConfig.coreAPI.userSearch.Request_Api_Url + "/u_id/" + u_id,
                method: 'POST',
                data: {
                    fields: ["u_leader"].join(',')
                }
            },
            function (data) {

                u_leader = data.result.data[0].u_leader;
                cb();

            }
        )
    }

    //查询父节点对应的用户
    function usersQueryingParentNodes(cb) {

        if (is_master > 0) {
            return cb();
        }

        R.SEND_HTTP(
            req,
            res,
            {
                url: CORE_APIConfig.coreAPI.userSearch.Request_Api_Url + "/u_id/" + u_leader,
                method: 'POST',
                data: {
                    fields: ["u_id", "u_nickname", "u_openid"].join(',')
                }
            },
            function (data) {

                u_fid = data.result.data[0].u_id;
                u_nickname = data.result.data[0].u_nickname;
                openid.push(data.result.data[0].u_openid);
                cb();

            }
        )
    }

    //新增反馈信息
    function feedbackAdd(cb) {

        var param = {
            sq_type: bodyParams.sq_type,
            sq_content: bodyParams.sq_content,
            sq_uid: bodyParams.u_id,
            sq_reply_uid: u_fid,
            sq_nickname: bodyParams.sq_nickname,
            sq_reply_nickname: u_nickname,
            sq_create_time: Date.parse(new Date()) / 1000,
        };

        R.SEND_HTTP(
            req,
            res,
            {
                url: CORE_APIConfig.coreAPI.artSalesQuestionAdd.Request_Api_Url,
                method: 'POST',
                data: param
            },
            function (data) {

                resultData.addResult = data.result;
                cb();

            }
        )
    }

    //查询销售
    function searchSaleInfo(cb) {

        if (is_master > 0) {
            R.SEND_HTTP(
                req,
                res,
                {
                    url: CORE_APIConfig.coreAPI.userSearch.Request_Api_Url + "/u_is_delete/0/u_id/" + bodyParams.c_uid,
                    method: 'POST',
                    data: {
                        fields: ['u_master_osid', 'u_is_manager', 'u_openid'].join(',')
                    }
                },
                function (data) {

                    if (data.result.data.length > 0) {
                        openid.push(data.result.data[0].u_openid);
                    }
                    cb();

                }
            )
        } else {

            cb();

        }
    }

    //查询代理
    function searchAgent(cb) {
        R.SEND_HTTP(
            req,
            res,
            {
                url: CORE_APIConfig.coreAPI.userSearch.Request_Api_Url + "/u_is_delete/0/u_type/2/u_master_osid/" + bodyParams.os_id,
                method: 'POST',
                data: {
                    fields: ['u_master_osid', 'u_is_manager', 'u_openid'].join(',')
                }
            },
            function (data) {

                if (data.result.data.length > 0) {
                    openid.push(data.result.data[0].u_openid);
                }
//              openid.push(data.result.data[0].u_openid);

                cb();

            }
        )
    }

    //发私信
    function sendPrivateMessage(cb) {

        F.SerialEach(openid, function (item, index, callback) {

            console.log("---------------");
            console.log(item);

            function loopSend(eachCb){
                if(item == 0 || item == ''){
                    eachCb();
                }else{
                    //添加反馈
                    var params = {
                        type: "fanKuiTiXing",
                        openid: item,
                        goUrls: "",//跳转
                        sq_id: resultData.addResult, //反馈ID
                        name: bodyParams.sq_nickname, //反馈人
                    };

                    R.SEND_HTTP(
                        req,
                        res,
                        {
                            url: config.server.ip + ":" + config.server.port + CORE_APIConfig.coreAPI.templateMessagesSent.Request_Api_Name,
                            method: 'POST',
                            data: params
                        },
                        function (data) {

                            console.log(data);

                            eachCb();

                        }
                    )
                }
            }

            F.Parallel([
                loopSend
            ], () => {
                callback();
            })

        }, cb)

    }

    /*//发私信
    function sendPrivateMessage(cb) {

        if (openid == 0 || openid == '') {
            cb();
        } else {

            //添加反馈
            var params = {
                type: "fanKuiTiXing",
                openid: openid,
                goUrls: "",//跳转
                sq_id: resultData.addResult, //反馈ID
                name: bodyParams.sq_nickname, //反馈人
            }

            R.SEND_HTTP(
                req,
                res,
                {
                    url: CORE_APIConfig.coreAPI.templateMessagesSent.Request_Api_Url,
                    method: 'POST',
                    data: params
                },
                function (data) {

                    console.log(data);

                    cb();

                }
            )

        }

    }*/


    F.Serial([
        queryParentNode,
        usersQueryingParentNodes,
        feedbackAdd,
        searchSaleInfo,
        searchAgent,
        sendPrivateMessage,
    ], () => {
        RES.successResponse(res, resultData);
    })


}